package com.wyz.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.wyz.entity.Admin;
import com.wyz.entity.Student;
import com.wyz.service.AdminService;
import com.wyz.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;

@Controller
public class TestController {
    @Autowired
    public StudentService studentService;
    @Autowired //自动注入业务类对象
    public AdminService adminService;
    @RequestMapping("/")
    public String start(){
        return "redirect:login.html";
    }

    @RequestMapping("/login")
    public String index(Model model, String name, String pwd, HttpSession session){
        Admin admin = adminService.login(name,pwd);
        if (admin!=null){
            model.addAttribute("admin",admin);
            session.setAttribute("admin",admin);
            return "redirect:index.html";
        }else {
            model.addAttribute("msg","账号或密码错误");
            return "redirect:login.html";
        }
    }
    @RequestMapping("/student")
    @ResponseBody
    public String student(){
        PageInfo<Student> all = studentService.findAll(1, 3, "", null);
        return all.getList().get(0).toString();
    }
    /**
     * 添加管理员的方法
     * @param admin
     * @return
     */
    @RequestMapping("addManager")
    @ResponseBody
    public int addManager(Admin admin, HttpServletRequest request) throws UnsupportedEncodingException {
        request.setCharacterEncoding("utf-8");
        admin = new Admin();
        admin.setId("11");
        admin.setName("小明");
        admin.setPhone("12345678");
        admin.setPassword("778899");
        admin.setAuthority(7);
        return adminService.addManager(admin);
       // return "redirect:addManager.html";
    }

    /**
     * 删除管理员的方法
     * @param id
     * @return
     */
    @RequestMapping("delManager")
    public String delManager(int id){
        adminService.delManager(id);
        return "redirect:delManager.html";
    }
    @RequestMapping("index")
    public String index1(){
        return "admin/admin";
    }


}
